package com.itzhanshen.javamail;

import javafx.application.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MySendMail {
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    JavaMailSender javaMailSender;
    // 简单发送
    @Test
    public void sendMail() {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(from);
        mailMessage.setTo("981555218@qq.com");
        //mailMessage.setCc("XXXX@qq.com");//抄送人
        mailMessage.setSubject("测试邮件");
        mailMessage.setText("我的测试");
        javaMailSender.send(mailMessage);
        System.out.println("[SUCCESS]");
    }

    // 带附件的发送
    @Test
    public void sendMainAffix() throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom(from);
        messageHelper.setTo("981555218@qq.com");
        messageHelper.setSubject("测试带有附件邮件");
        messageHelper.setText("附件邮件");
        messageHelper.addAttachment("1.xls",new File("D:\\1.xls"));
        javaMailSender.send(mimeMessage);
        System.out.println("[SUCCESS]");

    }
}
